<?php
/**
 * Created by PhpStorm.
 * User: davin
 * Date: 14-10-19
 * Time: 下午12:03
 *
 * 菜单类
 */

use LaravelBook\Ardent\Ardent;

class Menu extends Ardent{

    public $timestamps = false;

    public static $rules = array(
        'name' => 'required|min:2',
        'keyword' => 'required|min:4',
    );

    public static $relationsData = array(
        'parent' => array(self::BELONGS_TO, 'Menu', "foreignKey"=>'parent_id'),
        'children' => array(self::HAS_MANY, 'Menu',"foreignKey"=> 'parent_id',"otherKey"=> 'id'),
        'foods' => array(self::HAS_MANY, 'Food')
    );

    public static $column = array(
        'name' => '菜单名称',
        'keyword' => '关键字',
//        'parent_id' => '父菜单',
//        'children_count' => '子菜单数',
        'food_count' => '餐品数',
    );

    public static function boot()
    {
        parent::boot();

        self::deleting(function($entry){

            if($entry->hasChild()){
                $entry->errors()->add('error', '菜单下还有菜品，请移除菜品后尝试删除.');
                return false;
            }
            return true;
        });
    }

    public function hasChild(){
        return $this->children && $this->children()->count()>0;
    }

    public function hasFoods(){
        return $this->foods && $this->foods()->count()>0;
    }

    public function getParentName(){
        return $this->parent ? $this->parent->name : '顶级菜单';
    }

    public static function getCSVContent($entries){
        $output = '';
        foreach($entries as $result){
            foreach(array_keys(self::$column) as $column){
                foreach(array_keys(self::$column) as $column){
                    switch($column){
                        case 'parent_id':
                            $output = $output.$result->getParentName().',';
                            break;
                        case 'children_count':
                            $output = $output.$result->children()->count().',';
                            break;
                        case 'food_count':
                            $output = $output.$result->foods()->count().',';
                            break;
                        default:
                            $output = $output.$result->$column.',';
                            break;
                    }
                }
            }
            $output = $output."\r\n";
        }

        return $output;
    }

    public static function getExcelContent($entries){
        $output = '<tbody>';
        foreach($entries as $result){
            $output = $output.'<tr>';
            foreach(array_keys(self::$column) as $column){
                switch($column){
                    case 'parent_id':
                        $output = $output.'<td>'.$result->getParentName().'</td>';
                        break;
                    case 'children_count':
                        $output = $output.'<td>'.$result->children()->count().'</td>';
                        break;
                    case 'food_count':
                        $output = $output.'<td>'.$result->foods()->count().'</td>';
                        break;
                    default:
                        $output = $output.'<td>'.$result->$column.'</td>';
                        break;
                }
            }
            $output = $output.'</tr>';
        }

        return $output.'</tbody>';;
    }
}